var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var encryption = require('../../../utils/encryption.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    modifyIntegral: '',
    password:'',
    balance:'',
    isFocus: false,//控制input 聚焦
    wallets_password_flag: false,//密码输入遮罩
    disabled: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.balance = options.balance;
    this.setData({
      balance: this.data.balance
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  cancelExchangeIntegral: function () {
    wx.navigateBack();
  },
  exchangeIntegral:function(){
    if (this.data.modifyIntegral == ""){
      wx.showToast({
        title: '请输入金额',
        icon: 'succes',
        duration: 1000,
        mask: true
      })
    }else{
      this.setData({
        disabled: !this.data.disabled
      })
      pay(this);
    }
  },
  bindinputModifyIntegral: function (event) {
    var modifyIntegral = event.detail.value;
if (Number(modifyIntegral) > this.data.balance){
      wx.showToast({
        title: '余额不足',
        icon: 'succes',
        duration: 1000,
        mask: true
      })
      this.setData({
        modifyIntegral: ''
      });
      } else if (Number(modifyIntegral) == 0){
        this.setData({
          modifyIntegral: ''
        });
      }else{
      this.data.modifyIntegral = modifyIntegral
      this.setData({
        modifyIntegral: modifyIntegral
      });
    }
  },
  bindinputPassword: function (event) {
    var password = event.detail.value;
    this.setData({
      password: password
    });
  },
  //创建订单号
  createRecordSn: function () {
    var date = new Date()
    var Y = date.getFullYear()
    var M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    var ss = date.getMilliseconds()
    if (ss < 10) {
      ss = '00' + ss
    } else if (ss < 100) {
      ss = '0' + ss
    }
    // var result = Y.to + M + D + h + m + s + ss;
    var result = Y.toString() + M.toString() + D.toString() + h.toString() + m.toString() + s.toString() + ss.toString();
    console.log("result = " + result)
    return result
  },

  set_Focus() {//聚焦input
    console.log('isFocus', this.data.isFocus)
    this.setData({
      isFocus: true
    })
  },
  set_notFocus() {//失去焦点
    this.setData({
      isFocus: false
    })
  },
  set_wallets_password(e) {//获取钱包密码
    this.setData({
      wallets_password: e.detail.value
    });
    if (this.data.wallets_password.length == 6) {//密码长度6位时，自动验证钱包支付结果

      console.log("验证密码");
      var payPassword = this.data.wallets_password
      this.setData({
        payPassword: payPassword
      });
      wallet_pay(this)
      this.close_wallets_password()

    }
  },
  close_wallets_password() {//关闭钱包输入密码遮罩
    // this.data.disabled = true;
    this.setData({
      isFocus: false,//失去焦点
      wallets_password_flag: false,
      disabled: !this.data.disabled
    })
  },
  pay() {//去支付
    pay(this)
  }

})

function pay(_this) {
  console.log('钱包支付')
  _this.setData({
    wallets_password: ''
  });
  _this.setData({
    wallets_password_flag: true,
    isFocus: true
  })


  // console.log(_this.data)
  // console.log("payPassword = " + _this.data.payPassword)
}

// 钱包支付
function wallet_pay(_this) {
  console.log('钱包支付请求函数')
  let that = _this;
  var recordSn = that.createRecordSn()
  var modifyIntegral = that.data.modifyIntegral

  //  if (parseFloat(modifyAmount) > parseFloat(this.data.balance)) {
  //    wx.showToast({
  //      image: '/static/images/icon_error.png',
  //      title: "爱币不足"
  //    });
  //    return false;
  //  }
  util.request(api.WalletExchangeIntegral, {
    recordSn: encryption.encrypt(recordSn),
    modifyIntegral: encryption.encrypt(modifyIntegral),
    password: that.data.wallets_password

  }).then(function (res) {
    console.log(res);

    if (res.errno === 0) {

      wx.showModal({
        title: '提示',
        content: "积分兑换成功",
        success: function (res) {
          wx.navigateBack();
        },
        showCancel: false
      });

    } else {
      wx.showToast({
        image: '/static/images/icon_error.png',
        title: res.errmsg
      });
      return false;
    }
  });

  /*
  1.支付成功
  2.支付失败：提示；清空密码；自动聚焦isFocus:true，拉起键盘再次输入
  */
}